﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Xml.Linq;

namespace ServiceHost
{
    public class RouteHelper
    {
        const string GOOGLE_KEY = "";

        //http://maps.google.com/maps?f=d&hl=en&saddr=25.04202,121.534761&daddr=25.05202,121.554761&ie=UTF8&0&om=0&output=kml 

        //121.534760,25.042040,0.000000 121.533650,25.042190,0.000000 121.532950,25.042430,0.000000 121.532950,25.042430,0.000000 121.532980,25.044400,0.000000 121.532980,25.044400,0.000000 121.534000,25.044670,0.000000 121.534400,25.044820,0.000000 121.535690,25.045690,0.000000 121.536400,25.045990,0.000000 121.540510,25.046700,0.000000 121.543860,25.047490,0.000000 121.546430,25.048160,0.000000 121.549130,25.048140,0.000000 121.549130,25.048140,0.000000 121.550770,25.048110,0.000000 121.552570,25.048140,0.000000 121.552570,25.048140,0.000000 121.552700,25.049850,0.000000 121.552750,25.051440,0.000000 121.552650,25.051540,0.000000 121.552640,25.051680,0.000000 121.552640,25.051680,0.000000 121.552660,25.052280,0.000000 121.552660,25.052280,0.000000 121.553770,25.052260,0.000000 121.553770,25.052260,0.000000 121.554770,25.052240,0.000000 

        private static string FeedUrl = "http://maps.google.com/maps?f=d&hl=en&saddr={0},{1}&daddr={2},{3}&ie=UTF8&0&om=0&output=kml";

        public static Route[] GetSafeRoutes(HotSpot[] hotSpots, Town[] towns, Coordinate coordinate)
        {
            //TODO: Get routes for towns
            foreach (var town in towns)
            {
                //TODO: Improve performance
                var route = GetRoute(town.Coordinate, coordinate);
                
                //Check the route and return it if safe
                if (route != null)
                {
                    route.Town = town;
                    System.Diagnostics.Debug.WriteLine("Town: " + town.Name);
                
                    if (route.IsSafe(hotSpots))
                    {
                        //TODO: Return more than one route
                        return new Route[1] {route};
                    }

                    System.Diagnostics.Debug.WriteLine("Route is not safe");
                }
            }

            return null;
        }

        //http://stackoverflow.com/questions/1397563/getting-coordinates-and-names-for-a-driving-route
        //http://stackoverflow.com/questions/6653623/how-can-i-get-coordinates-in-the-route-path-in-google-map
        public static Route GetRoute(Coordinate destination, Coordinate source)
        {
            XDocument feedXml = null;

            string apiUrl = string.Format(FeedUrl, source.Y, source.X, destination.Y, destination.X);
           
            try
            {
                System.Diagnostics.Debug.WriteLine("Uri: " + apiUrl);
                feedXml = XDocument.Load(apiUrl);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("Failed to loaded feed: " + ex.Message);
            }

            if (feedXml == null)
                return null;

            System.Diagnostics.Debug.WriteLine("Getting latest feed");
            
            //TODO: Add proper parsing and validation
            var a = feedXml.Descendants().Where(x => x.Name.LocalName == "LineString");//(from element in feedXml.Descendants("LineString") select element.Attribute("coordinates").Value).First();

            string s = a.First().Value;

            if (s == string.Empty)
            {
                return null;
            }

            System.Diagnostics.Debug.WriteLine("Got polyline {0}", s);

            //TODO: Convert polyline to Route
            var route = new Route(s);

            return route;
        }

    }
}